<?php

class encoder
{
    static function getSupported()
    {
        return array('deflate', 'gzip', 'x-gzip');
    }

    static function encode($octets, $encoding)
    {
        if (! is_string($octets)) {
            throw new ArgException();
        }
        if (! is_string($encoding)) {
            throw new ArgException();
        }
        $encoding = trim(strtolower($encoding));
        return self::getInstance()->_encode($octets, $encoding);
    }

    static function decode($octets, $encoding)
    {
        if (! is_string($octets)) {
            throw new ArgException();
        }
        if (! is_string($encoding)) {
            throw new ArgException();
        }
        $encoding = trim(strtolower($encoding));
        return self::getInstance()->_decode($octets, $encoding);
    }

    protected function _encode($octets, $encoding)
    {
        switch ($encoding) {

        case 'x-gzip':
        case 'gzip':
            return gzencode($octets, 9, FORCE_GZIP);

        case 'deflate':
            return gzdeflate($octets, 9);

        default:
            throw new ArgException();
        }
    }

    protected function _decode($octets, $encoding)
    {
        switch ($encoding) {

        case 'x-gzip':
        case 'gzip':
            return gzdecode($octets);

        case 'deflate':
            return gzinflate($octets);

        default:
            throw new ArgException();
        }
    }

    static private function &getInstance()
    {
        static $instance = null;
        if (is_null($instance)) {
            $instance = new self();
        }

        return $instance;
    }

    private function __construct() {}
}

?>